package com.heo.protocol.packet;

import org.apache.log4j.Logger;

import com.heo.util.ByteHelper;

public class GetDeviceParaReq extends BasePacket
{
  private int BODY_LENGTH = 74;// 包体长74个字节
  private static final String PACKET_NAME = "[获取设备参数请求包]";

  private String hardVer;// 硬件版本号，4个字节
  private String simId;// sim卡id，30个字节
  private String deviceSn;// 设备sn号，20个字节
  private String deviceMac;// 设备mac地址，20个字节

  private static Logger log = Logger.getLogger(GetDeviceParaReq.class);

  /**
   * 通过网络传过来的流构建对象
   * 
   * @param totalLength
   * @param commandId
   * @param sequenceId
   * @param body
   * @throws Exception
   */
  public GetDeviceParaReq(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }

  public GetDeviceParaReq() throws Exception
  {
    super();
    super.commandId = DeviceCommand.GET_DEVICE_PARA_REQ;
    super.sequenceId = 0x01;
    super.totalLength = HEAD_LENGTH + BODY_LENGTH;
  }

  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + BODY_LENGTH;
  }

  @Override
  public byte[] getPacketBody()
  {
    return ByteHelper.margeByte(hardVer.getBytes(), ByteHelper.stringToByteArray(simId, 30), ByteHelper.stringToByteArray(deviceSn, 20), ByteHelper.stringToByteArray(deviceMac, 20), null, null, null, null, null, null);
  }

  @Override
  public void makeBody() throws Exception
  {
    if (packetBody.length != BODY_LENGTH)
    {
      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
    }
    hardVer = ByteHelper.byteToString(packetBody, 0, 4, (byte) 0);
    simId = ByteHelper.byteToString(packetBody, 4, 30, (byte) 0);
    deviceSn = ByteHelper.byteToString(packetBody, 34, 20, (byte) 0);
    deviceMac = ByteHelper.byteToString(packetBody, 54, 20, (byte) 0);
    log.debug(super.logHeader + super.packetName + "数据包解析成功。");
  }

  public static void main(String[] args)
  {
    try
    {
      GetDeviceParaReq gdpr = new GetDeviceParaReq();
      gdpr.setCommandId((short) 0x0005);
      gdpr.setDeviceMac("1122334455");
      gdpr.setDeviceSn("1111111111");
      gdpr.setHardVer("1.00");
      gdpr.setSendCount((short) 1);
      gdpr.setSequenceId(1);
      gdpr.setSimId("2222222222");
      System.out.println(gdpr.toString());
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

  }

  public String getHardVer()
  {
    return hardVer;
  }

  public void setHardVer(String hardVer)
  {
    this.hardVer = hardVer;
  }

  public String getSimId()
  {
    return simId;
  }

  public void setSimId(String simId)
  {
    this.simId = simId;
  }

  public String getDeviceSn()
  {
    return deviceSn;
  }

  public void setDeviceSn(String deviceSn)
  {
    this.deviceSn = deviceSn;
  }

  public String getDeviceMac()
  {
    return deviceMac;
  }

  public void setDeviceMac(String deviceMac)
  {
    this.deviceMac = deviceMac;
  }

}
